from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support.select import Select




def open_browser(browser : str ='Chrome'):
    if browser.capitalize() == 'Chrome':
        return webdriver.Chrome()
    elif browser.capitalize() == 'Chrome_with_opt':
        # 添加个人资料路径
        user_data = r'--user-data-dir=C:\Users\Administrator\AppData\Local\Google\Chrome\User Data'
        # 将个人资料路径添加到谷歌浏览器加载项中
        options = webdriver.ChromeOptions() # 实例化一个对象
        options.add_argument(user_data) # 调用方法，传递参数
        # 将携带加载项的浏览器对象 返回
        return webdriver.Chrome(options=options)
    elif browser.capitalize() == 'Chrome_with_headless':
        options = webdriver.ChromeOptions()
        options.add_argument("--headless")
        return webdriver.Chrome(options=options)
    elif browser.capitalize() == 'Firefox':
        return webdriver.Firefox()
    elif browser.capitalize() == 'Edge':
        return webdriver.Edge(r'C:\Users\Administrator\AppData\Local\Programs\Python\Python37\msedgedriver.exe')
    else:
        print('请输入正确的浏览器名称，目前仅支持Chrome、Firefox、Edge')

class Base:

    def __init__(self,driver):
        self.driver = driver

    # 访问网址
    def get_url(self,url):
        try:
            self.driver.get(url)
            self.driver.maximize_window()
            self.driver.implicitly_wait(10)
        except:
            print('浏览器打开失败，如法输入网址')

    # 退出浏览器
    def close_browser(self):
        try:
            self.driver.quit()
        except:
            print('浏览器关闭失败')

    # 定位一个元素
    def find_element(self,locator):
        try:
            return WebDriverWait(self.driver, 10, 2).until(EC.presence_of_element_located(locator))
        except:
            print(f'元素{locator}未找到')

    # 定位一组元素
    def find_elements(self,locator):
        try:
            return WebDriverWait(self.driver, 10, 2).until(EC.presence_of_all_elements_located(locator))
        except:
            print(f'元素{locator}未找到')

    # 输入文本
    def send_keys(self,locator,text):
        try:
            element = self.find_element(locator)
            element.clear()
            element.send_keys(text)
        except:
            print(f'元素{locator}未找到')

    # 点击
    def click(self,locator):
        try:
            element = self.find_element(locator)
            element.click()
        except:
            print(f'元素{locator}未找到')

    # 获取元素的文本值
    def get_element_text(self,locator):
        try:
            element = self.find_element(locator)
            return element.text
        except:
            print(f'元素{locator}没有找到')

    # 获取元素某个属性的属性值
    def get_element_value(self,locator,attribute):
        try:
            element = self.find_element(locator)
            return element.get_attribute(attribute)
        except:
            print(f'元素{locator}没有找到')

    # 获取html页面资源
    def get_page_source(self):
        return self.driver.page_source

    # 通过js脚本，打开一个新窗口
    def open_new_window(self,info_url):
        js = 'window.open("%s")' % info_url
        self.driver.execute_script(js)

    # 多窗口切换，默认切换到最新打开的窗口
    def swtich_to_window(self,info_index:int = -1):
        all_windows = self.driver.window_handles
        self.driver.switch_to.window(all_windows[info_index])

    # 下拉选择框，通过value值处理,ele直接接收定位到的元素
    def select_by_value(self,ele,info_value):
        Select(ele).select_by_value(info_value)

    # 下拉选择框，通过索引号处理，ele直接接收定位到的元素
    def select_by_index(self,ele,info_index : str):
        Select(ele).select_by_index(info_index)

    # 进入iframe标签
    def enter_frame(self,locator):
        info_frame = self.find_element(locator)
        self.driver.switch_to.frame(info_frame)

    # 退出iframe标签，至最外层 （如果需要，可以封装逐级跳出的代码）
    def quit_frame_to_default_content(self):
        self.driver.switch_to.default_content()

    # 截图
    def get_screenshot_as_file(self,path):
        self.driver.get_screenshot_as_file(path)

    # 刷新页面
    def refresh_window(self):
        self.driver.refresh()



if __name__ == '__main__':
    base = Base(open_browser('firefox'))
    base.get_url('http://localhost/shop/')
    search_locator = (By.NAME,'word')
    base.send_keys(search_locator,'奥特曼')
    button_locator = (By.CLASS_NAME,'btn')
    base.click(button_locator)
    sleep(5)
    base.close_browser()
